#include "UserInputDataProcessor.h"
const size_t UserInputDataProcessor::PARALLEL_SORT_THRESHOLD = 200;
const size_t UserInputDataProcessor::PARALLEL_BUILD_THRESHOLD = 200;

UserInputDataProcessor::UserInputDataProcessor()
    :flightNum(0), queryPointNum(0), currentLine(1)
{
    FlightData.resize(1); QueryPointData.resize(1); //QueryPointData_Point.resize(1);
    FlightData[0].emplace_back(L"飞行线编号"); FlightData[0].emplace_back(L"飞行线端点1坐标"); FlightData[0].emplace_back(L"飞行线端点2坐标"); //FlightData[0].emplace_back(L"查询点到飞行线距离");
    QueryPointData[0].emplace_back(L"查询点坐标"); QueryPointData[0].emplace_back(L"距离最近的飞行线编号"); QueryPointData[0].emplace_back(L"距离大小");
zz
}

int UserInputDataProcessor::getCurrentLine() {
    return currentLine;
}

void UserInputDataProcessor::getFlightDataReference(vector<vector<wstring>>& newdata) {
    newdata = FlightData;
}

void UserInputDataProcessor::getQueryPointDataReference(vector<vector<wstring>>& newdata) {
    newdata = QueryPointData;
}

void UserInputDataProcessor::getQueryPointData_PointReference(vector<vector<wstring>>& newdata) {
    newdata.resize(queryPointNum + 1);
    newdata[0].emplace_back(L"查询点坐标");
    for (int i = 1; i <= queryPointNum; ++i) {
        newdata[i].emplace_back(QueryPointData[i][0]);
    }
    return;
}



pair<double, double> UserInputDataProcessor::findClosestPointOnSegment(double Px, double Py,
    double Ax, double Ay,
    double Bx, double By) const {
    // 向量AB
    double ABx = Bx - Ax;
    double ABy = By - Ay;

    // 向量AP
    double APx = Px - Ax;
    double APy = Py - Ay;

    // 计算AB长度的平方
    double AB2 = ABx * ABx + ABy * ABy;

    // 计算AP与AB的点积
    double AP_AB = APx * ABx + APy * ABy;

    // 计算参数t，表示最近点在线段AB上的位置比例
    double t = AP_AB / AB2;

    // 检查最近点是否在线段上
    if (t <= 0.0) {
        // 最近点是A点
        return make_pair(Ax, Ay);
    }
    else if (t >= 1.0) {
        // 最近点是B点
        return make_pair(Bx, By);
    }
    else {
        // 垂足在线段上
        return make_pair(Ax + t * ABx, Ay + t * ABy);
    }
}

// 共用的数据结构初始化
void UserInputDataProcessor::initDataStructures(int flight_num,int query_point_num,bool resizeFlightLines){
    int final_flight_num = (flight_num == -1) ? flightNum : flight_num;
    int final_query_num = (query_point_num == -1) ? queryPointNum : query_point_num;

    FlightData.clear();
    FlightData.reserve(final_flight_num + 1);
    FlightData.push_back({ L"飞行线编号",L"飞行线端点1坐标" ,L"飞行线端点2坐标" });
    //for (int i = 1; i < FlightData.size(); ++i) {
    //    FlightData[i].clear();
    //}
    QueryPointData.clear();
    QueryPointData.reserve(final_query_num + 1);
    QueryPointData.push_back({ L"查询点坐标" ,L"距离最近的飞行线编号" ,L"距离大小" });
    //for (int i = 1; i < QueryPointData.size(); ++i) {
    //    QueryPointData[i].clear();
    //}

    if (resizeFlightLines) {
        FlightLines.clear();
        //FlightLines.reserve(flightNum);
        FlightLines.resize(final_flight_num);
        QueryPoints.clear();
        //QueryPoints.reserve(queryPointNum);
        QueryPoints.resize(final_query_num);
    }
}

void UserInputDataProcessor::processFlightLine(int id, double x1, double y1, double x2, double y2) {
    FlightLines[id - 1] = { id, x1, y1, x2, y2 };
    // 正确使用 emplace_back：直接传递构造函数参数
    //FlightLines.emplace_back(id, x1, y1, x2, y2);
    wstring xy1 = L"(" + formatCoord(x1)+ L"," + formatCoord(y1) + L")";
    wstring xy2 = L"(" + formatCoord(x2)+ L"," + formatCoord(y2) + L")";
    //FlightData.emplace_back(to_wstring(id), xy1, xy2);
    FlightData.emplace_back(std::initializer_list<wstring>{
        to_wstring(id),    // 飞行线编号
            xy1,               // 端点1坐标
            xy2                // 端点2坐标
    });

    //FlightData[id].emplace_back(to_wstring(id));
    //FlightData[id].emplace_back(xy1);
    //FlightData[id].emplace_back(xy2);
}

void UserInputDataProcessor::processQueryPoint(int id, double x, double y) {
    QueryPoints[id - 1] = { x, y };
    //QueryPoints.emplace_back(x, y);
    wstring xy = L"(" + formatCoord(x) + L"," + formatCoord(y)+ L")";
    QueryPointData.emplace_back(std::initializer_list<wstring>{xy});
}

int UserInputDataProcessor::readDataFile(wstring filePath) {
    currentLine = 1;
    //判断传入的文件路径是不是.txt或.csv
    if (!isSupportedFile(filePath)) return -1;//如果不是则提示用户传入.txt或.csv文件
    wifstream file(filePath);
    if (!file.is_open()) {
        return 0; // 文件打开失败
    }
    try {
        // 根据扩展名处理
        size_t dotPos = filePath.find_last_of(L".");
        wstring ext = filePath.substr(dotPos + 1);
        transform(ext.begin(), ext.end(), ext.begin(), ::towlower);

        if (ext == L"txt") {
            return readTextFile(file);
        }
        else if (ext == L"csv") {
            return readCSVFile(file);
        }
    }
    catch (...) {
        return -404; // 未知错误
    }
    return -404;  
    // return 0;  文件打开失败
    // return -1; 不是.txt或.csv
    // return -2; 第i行数据为空!
    // return -3; 第i行数据数量不足
    // return -4; 数据存在非法字符
    // return -5; 数值转换失败
}
// return 0;  文件打开失败
// return -1; 不是.txt或.csv
// return -2; 第i行数据为空!
// return -3; 第i行数据数量不足
// return -4; 第i行数据数量过多
// return -5; 数据存在非法字符
// return -6  数据出现负号
// CSV行分割辅助函数
vector<wstring> UserInputDataProcessor::splitCSVLine(const wstring& line) {
   vector<wstring> result;
   wstringstream ss(line);
   wstring field;

     while (getline(ss, field, L',')) {
        // 去除首尾空格
        size_t start = field.find_first_not_of(L" ");
        size_t end = field.find_last_not_of(L" ");
        if (start != wstring::npos && end != wstring::npos) {
            result.emplace_back(field.substr(start, end - start + 1));
        } else {
            result.emplace_back(L""); // 空字段
        }
    }
    return result;
}

vector<wstring> UserInputDataProcessor::splitTXTLine(const wstring& line) {
    vector<wstring> result;
    size_t start = 0, end = 0;
    const size_t len = line.length();

    // 预分配内存（假设每行最多40个字段）
    result.reserve(40);

    while (start < len) {
        // 跳过前导空格
        while (start < len && line[start] == L' ') start++;
        if (start >= len) break;

        // 找到字段结尾
        end = start;
        while (end < len && line[end] != L' ') end++;

        // 直接构造字段（避免substr复制）
        result.emplace_back(line.data() + start, end - start);

        start = end + 1;
    }
    return result;
}
/*
vector<wstring> UserInputDataProcessor::splitTXTLine(const wstring& line) {
    vector<wstring> result;
    wstringstream ss(line);
    wstring field;

    while (getline(ss, field, L' ')) {
        // 去除首尾空格
        size_t start = field.find_first_not_of(L" ");
        size_t end = field.find_last_not_of(L" ");
        if (start != wstring::npos && end != wstring::npos) {
            result.emplace_back(field.substr(start, end - start + 1));
        }
        //else {
        //    result.emplace_back(L""); // 空字段
        //}
    }
    return result;
}*/

/*
void UserInputDataProcessor::splitTXTLine(const wstring& line, vector<wstring>& result) {
    //vector<wstring> result;
    size_t start = 0, end = 0;
    const size_t len = line.length();

    // 预分配内存（假设每行最多40个字段）
    result.reserve(40);

    while (start < len) {
        // 跳过前导空格
        while (start < len && line[start] == L' ') start++;
        if (start >= len) break;

        // 找到字段结尾
        end = start;
        while (end < len && line[end] != L' ') end++;

        // 直接构造字段（避免substr复制）
        result.emplace_back(line.data() + start, end - start);

        start = end + 1;
    }

}
*/
int UserInputDataProcessor::readCSVFile(wifstream& file) {
    wstring line;
    // 读取首行：飞行线数量,查询点数量
    if (!getline(file, line)) return -2;
    vector<wstring> header = splitCSVLine(line);
    if (header.size() < 2) return -3;
    else if (header.size() > 2) return -4;
    try {
        // 先验证格式再转换
        if (!isValidNumber(header[0])) return -5;
        if (!isValidNumber(header[1])) return -5;

        flightNum = stoi(header[0]);
        queryPointNum = stoi(header[1]);
        //if (flightNum < 0 || queryPointNum < 0) return -6;
        ++currentLine;
    }
    catch (...) {
        return -7; // 数值转换失败
    }
    // 验证数量有效性


    //// 初始化数据结构
    initDataStructures();

    // 读取飞行线数据
    for (int i = 1; i <= flightNum; ++i) {
        if (!getline(file, line)) return -2;
        vector<wstring> parts = splitCSVLine(line);
        if (parts.size() < 4) return -3;
        else if (parts.size() > 4) return -4;
        if (!isValidNumber2(parts[0])) return -5;
        if (!isValidNumber2(parts[1])) return -5;
        if (!isValidNumber2(parts[2])) return -5;
        if (!isValidNumber2(parts[3])) return -5;
        try {
            double x1 = stod(parts[0]);
            double y1 = stod(parts[1]);
            double x2 = stod(parts[2]);
            double y2 = stod(parts[3]);
            processFlightLine(i, x1, y1, x2, y2);
            ++currentLine;
        }
        catch (...) {
            return -7; // 坐标转换失败
        }
    }

    // 读取查询点数据
    for (int i = 1; i <= queryPointNum; ++i) {
        if (!getline(file, line)) return -2;
        vector<wstring> parts = splitCSVLine(line);
        if (parts.size() < 2) return -3;
        else if (parts.size() > 2) return -4;
        if (!isValidNumber2(parts[0])) return -5;
        if (!isValidNumber2(parts[1])) return -5;
        try {
            double x = stod(parts[0]);
            double y = stod(parts[1]);
            processQueryPoint(i, x, y); //查询点坐标
            ++currentLine;
        }
        catch (...) {
            return -7; // 坐标转换失败
        }
    } 
    findNearestFlightLineParallel(FlightLines, QueryPoints);
    return 1;
}

int UserInputDataProcessor::readTextFile(wifstream& file) {
    wstring line;

    if (!getline(file, line)) return -2;
    //wistringstream header(line);
    vector<wstring> header = splitTXTLine(line);
    if (header.size() < 2) return -3;
    else if (header.size() > 2) return -4;
    try {
        // 先验证格式再转换
        if (!isValidNumber(header[0])) return -5;
        if (!isValidNumber(header[1])) return -5;

        flightNum = stoi(header[0]);
        queryPointNum = stoi(header[1]);
        ++currentLine;
    }
    catch (...) {
        return -7; // 数值转换失败
    }
    // 开始计时
    auto start0 = std::chrono::high_resolution_clock::now();
    initDataStructures();
    // 结束计时
    auto end0 = std::chrono::high_resolution_clock::now();
    // 计算耗时（毫秒）
    auto duration0 = std::chrono::duration_cast<std::chrono::milliseconds>(end0 - start0).count();
    std::cout << "初始化数组执行时间: " << duration0 << " 毫秒" << std::endl;
    // 开始计时
    auto start1 = std::chrono::high_resolution_clock::now();
        for (int i = 1; i <= flightNum; ++i) {
            if (!getline(file, line)) return -2;
            vector<wstring> parts = splitTXTLine(line);
            if (parts.size() < 4) return -3;
            else if (parts.size() > 4) return -4;
            if (!isValidNumber2(parts[0])) return -5;
            if (!isValidNumber2(parts[1])) return -5;
            if (!isValidNumber2(parts[2])) return -5;
            if (!isValidNumber2(parts[3])) return -5;
            try {
                double x1 = stod(parts[0]);
                double y1 = stod(parts[1]);
                double x2 = stod(parts[2]);
                double y2 = stod(parts[3]);
                processFlightLine(i, x1, y1, x2, y2);
                ++currentLine;
            }
            catch (...) {
                return -7; // 坐标转换失败
            }
           
        }
    // 结束计时
    auto end1 = std::chrono::high_resolution_clock::now();
    // 计算耗时（毫秒）
    auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1- start1).count();
    std::cout << "传入飞行线数据执行时间: " << duration1 << " 毫秒" << std::endl;

    // 开始计时
    auto start2 = std::chrono::high_resolution_clock::now();
    for (int i = 1; i <= queryPointNum; ++i) {
        if (!getline(file, line)) return -2;
        vector<wstring> parts = splitTXTLine(line);
        if (parts.size() < 2) return -3;
        else if (parts.size() > 2) return -4;
        if (!isValidNumber2(parts[0])) return -5;
        if (!isValidNumber2(parts[1])) return -5;
        try {
            double x = stod(parts[0]);
            double y = stod(parts[1]);
            processQueryPoint(i, x, y); //查询点坐标
            ++currentLine;
        }
        catch (...) {
            return -7; // 坐标转换失败
        }

    }
    // 结束计时
    auto end2 = std::chrono::high_resolution_clock::now();
    // 计算耗时（毫秒）
    auto duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(end2 - start2).count();
    std::cout << "传入查询点数据执行时间: " << duration2 << " 毫秒" << std::endl;


     // 开始计时
    auto start3 = std::chrono::high_resolution_clock::now();
    findNearestFlightLineParallel(FlightLines, QueryPoints);
    // 结束计时
    auto end3= std::chrono::high_resolution_clock::now();
    // 计算耗时（毫秒）
    auto duration3 = std::chrono::duration_cast<std::chrono::milliseconds>(end3 - start3).count();
    std::cout << "KD树计算数据执行时间: " << duration3 << " 毫秒" << std::endl;
    std::cout << "总执行时间: " << duration0+duration1+ duration2+ duration3 << " 毫秒" << std::endl;
    return 1; //返回1表示成功
}

void UserInputDataProcessor::finishDirectInput(){
    int linenumber = DirectlyInputFlightLines.size();
    int quertnumber = DirectlyInputQueryPoints.size();

    flightNum = linenumber;
    queryPointNum = quertnumber;
    initDataStructures();

    for (int i = 1; i <=linenumber; ++i) {

        FlightLines[i - 1] = {
            i, 
            DirectlyInputFlightLines[i-1].x1,
            DirectlyInputFlightLines[i-1].y1,
            DirectlyInputFlightLines[i-1].x2,
            DirectlyInputFlightLines[i-1].y2
        };
        //FlightLines[i-1].id = i;

        wstring xy1 = L"(" + formatCoord(DirectlyInputFlightLines[i-1].x1) + L"," + formatCoord(DirectlyInputFlightLines[i-1].y1) + L")";
        wstring xy2 = L"(" + formatCoord(DirectlyInputFlightLines[i-1].x2) + L"," + formatCoord(DirectlyInputFlightLines[i-1].y2) + L")";
        FlightData[i].emplace_back(to_wstring(i));
        FlightData[i].emplace_back(xy1);
        FlightData[i].emplace_back(xy2);
    }
    for (int i = 1; i <= quertnumber; ++i) {
        QueryPoints[i - 1] = { DirectlyInputQueryPoints[i-1].x, DirectlyInputQueryPoints[i-1].y};

        wstring xy = L"(" + formatCoord(DirectlyInputQueryPoints[i-1].x) + L"," + formatCoord(DirectlyInputQueryPoints[i-1].y) + L")";
        QueryPointData[i].emplace_back(xy);
    }
    findNearestFlightLineParallel(FlightLines, QueryPoints);
}






//KD树处理代码
pair<size_t, double> UserInputDataProcessor::findSplit(vector<FlightLine>& flightLines, int axis) const {
    auto compare = [axis](const FlightLine& a, const FlightLine& b) {
        auto bbA = a.getBoundingBox();
        auto bbB = b.getBoundingBox();
        double aVal = axis == 0 ? bbA.first.first : bbA.first.second;
        double bVal = axis == 0 ? bbB.first.first : bbB.first.second;
        return aVal < bVal;
        };

    size_t mid = flightLines.size() / 2;
    nth_element(flightLines.begin(), flightLines.begin() + mid, flightLines.end(), compare);

    auto bb = flightLines[mid].getBoundingBox();
    double splitVal = axis == 0 ? bb.first.first : bb.first.second;

    return { mid, splitVal };
}

shared_ptr<UserInputDataProcessor::KDNode> UserInputDataProcessor::buildKDTreeParallel(vector<FlightLine>& flightLines, int depth, bool parallel) const {
    if (flightLines.empty()) return nullptr;

    int axis = depth % 2;
    bool useParallel = parallel && flightLines.size() > PARALLEL_BUILD_THRESHOLD;

    auto result = findSplit(flightLines, axis);
    size_t mid = result.first;
    double splitVal = result.second;

    auto node = make_shared<KDNode>(flightLines[mid], axis, splitVal);

    vector<FlightLine> leftLines(make_move_iterator(flightLines.begin()), make_move_iterator(flightLines.begin() + mid));
    vector<FlightLine> rightLines(make_move_iterator(flightLines.begin() + mid + 1), make_move_iterator(flightLines.end()));

    if (useParallel) {
        shared_ptr<KDNode> leftNode, rightNode;
        thread leftThread([&]() { leftNode = buildKDTreeParallel(leftLines, depth + 1, false); });
        rightNode = buildKDTreeParallel(rightLines, depth + 1, false);
        leftThread.join();

        node->left = leftNode;
        node->right = rightNode;
    }
    else {
        node->left = buildKDTreeParallel(leftLines, depth + 1, false);
        node->right = buildKDTreeParallel(rightLines, depth + 1, false);
    }

    node->updateBoundingBox();
    return node;
}

double UserInputDataProcessor::pointToLineSquaredDistance(double px, double py, const FlightLine& line) const {
    double x1 = line.x1, y1 = line.y1;
    double x2 = line.x2, y2 = line.y2;

    double dx = x2 - x1;
    double dy = y2 - y1;
    double lineLengthSq = dx * dx + dy * dy;

    if (lineLengthSq == 0) {
        return (px - x1) * (px - x1) + (py - y1) * (py - y1);
    }

    double t = ((px - x1) * dx + (py - y1) * dy) / lineLengthSq;
    t = max(0.0, min(1.0, t));

    double projectionX = x1 + t * dx;
    double projectionY = y1 + t * dy;

    return (px - projectionX) * (px - projectionX) + (py - projectionY) * (py - projectionY);
}

double UserInputDataProcessor::pointToBBoxMinSquaredDistance(double px, double py, const pair<pair<double, double>, pair<double, double>>& bbox) const {
    double dx = max({ bbox.first.first - px, 0.0, px - bbox.second.first });
    double dy = max({ bbox.first.second - py, 0.0, py - bbox.second.second });
    return dx * dx + dy * dy;
}

void UserInputDataProcessor::searchKDTree(shared_ptr<KDNode> node, const QueryPoint& query, double& minSqDist, int& nearestId) const {
    if (!node) return;

    double nodeMinSqDist = pointToBBoxMinSquaredDistance(query.x, query.y, node->bbox);
    if (nodeMinSqDist > minSqDist) return;

    double currentSqDist = pointToLineSquaredDistance(query.x, query.y, node->line);
    if (currentSqDist < minSqDist) {
        minSqDist = currentSqDist;
        nearestId = node->id;
    }
    else if (currentSqDist == minSqDist && node->id < nearestId) {
        nearestId = node->id;
    }

    double queryVal = node->axis == 0 ? query.x : query.y;
    bool goLeftFirst = queryVal < node->splitValue;

    if (goLeftFirst) {
        searchKDTree(node->left, query, minSqDist, nearestId);
        if ((queryVal - sqrt(minSqDist)) <= node->splitValue) {
            searchKDTree(node->right, query, minSqDist, nearestId);
        }
    }
    else {
        searchKDTree(node->right, query, minSqDist, nearestId);
        if ((queryVal + sqrt(minSqDist)) >= node->splitValue) {
            searchKDTree(node->left, query, minSqDist, nearestId);
        }
    }
}

void UserInputDataProcessor::parallelSearch(const shared_ptr<KDNode>& root,const vector<QueryPoint>& queryPoints,
    size_t start,
    size_t end
) {
    for (size_t i = start; i < end; ++i) {
        double minSqDist = numeric_limits<double>::max();
        int nearestId = -1;
        searchKDTree(root, queryPoints[i], minSqDist, nearestId);
        //results[i] = nearestId;

        // 计算实际距离并转换为wstring
        double exactMinDist = sqrt(minSqDist);
        size_t dataIndex = i + 1;
        QueryPointData[dataIndex].emplace_back(to_wstring(nearestId));       // 修改的代码 - 存入ID
        QueryPointData[dataIndex].emplace_back(to_wstring(exactMinDist));    // 修改的代码 - 存入距离
    }
}

void UserInputDataProcessor::findNearestFlightLineParallel(const vector<FlightLine>& flightLines, const vector<QueryPoint>& queryPoints) {
    if (flightLines.empty()) {
        // 可选：处理无飞行线的情况（例如填充默认值）
        for (size_t i = 1; i < QueryPointData.size(); ++i) {
            QueryPointData[i].emplace_back( L"-1"); // ID
            QueryPointData[i].emplace_back(L"INF"); // 距离
        }
        return;
    }

    vector<FlightLine> linesCopy = flightLines;
    auto root = buildKDTreeParallel(linesCopy);

    //vector<int> results(queryPoints.size());

    if (queryPoints.size() < 200) {
        parallelSearch(root, queryPoints, 0, queryPoints.size());
    }
    else {
        const size_t numThreads = thread::hardware_concurrency();
        vector<thread> threads;
        size_t chunkSize = queryPoints.size() / numThreads;

        for (size_t i = 0; i < numThreads; ++i) {
            size_t start = i * chunkSize;
            size_t end = (i == numThreads - 1) ? queryPoints.size() : start + chunkSize;
            threads.emplace_back(
                &UserInputDataProcessor::parallelSearch,
                this,
                root,
                cref(queryPoints),
                start,
                end
            );
        }

        for (auto& t : threads) {
            t.join();
        }
    }
}

/*
原来的代码
/*
void UserInputDataProcessor::parallelSearch(const shared_ptr<KDNode>& root, const vector<QueryPoint>& queryPoints, vector<int>& results, size_t start, size_t end) const {
    for (size_t i = start; i < end; ++i) {
        double minSqDist = numeric_limits<double>::max();
        int nearestId = -1;
        searchKDTree(root, queryPoints[i], minSqDist, nearestId);
        results[i] = nearestId;
    }
}*/




//vector<FlightLine> linesCopy = flightLines;
    //auto root = buildKDTree(linesCopy);
    //int num = 1; //后加入的代码
    //vector<int> results;
    //for (const auto& qp : queryPoints) {
    //    #undef max 
    //    double minDist = numeric_limits<double>::max();
    //    int nearestId = -1;
    //    // 先用KD树找到最近的中点（近似解）
    //    nearestNeighborSearch(root, qp, 0, minDist, nearestId);
    //    // 由于KD树存储的是中点，我们需要在所有飞行线中精确计算距离
    //    // 但可以只检查KD树返回的候选飞行线，减少计算量
    //    #undef max 
    //    double exactMinDist = numeric_limits<double>::max();
    //    int exactNearestId = -1;
    //    for (const auto& line : flightLines) {
    //        double dist = pointToLineDistance(qp.x, qp.y, line.x1, line.y1, line.x2, line.y2);
    //        if (dist < exactMinDist) {
    //            exactMinDist = dist;
    //            exactNearestId = line.id;
    //        }
    //    }
    //    QueryPointData[num].emplace_back(to_wstring(exactNearestId));  //后加入的代码
    //    QueryPointData[num].emplace_back(to_wstring(exactMinDist));//后加入的代码
    //    results.emplace_back(exactNearestId);
    //    ++num; //后加入的代码
    //}
    //return results;
//暴力方法求解最近飞行线
//void UserInputDataProcessor::findNearestFlightLine(const vector<FlightLine>& flightLines, const vector<QueryPoint>& queryPoints) {
//    vector<int> results;
//    int num = 1; //后加入的代码
//    for (const auto& qp : queryPoints) {
//        double minDistance = 1.7976931348623157e+308;
//        int nearestLineId = -1;
//        for (const auto& line : flightLines) {
//            double distance = pointToLineDistance(qp.x, qp.y, line.x1, line.y1, line.x2, line.y2);
//            if (distance < minDistance) {
//                minDistance = distance;
//                nearestLineId = line.id;
//            }
//        }               
//        QueryPointData[num].emplace_back(to_wstring(nearestLineId));  //后加入的代码
//        QueryPointData[num].emplace_back(to_wstring(minDistance));//后加入的代码
//        results.emplace_back(nearestLineId);
//        ++num; //后加入的代码
//    }
//    return;
//    //return results;
//}
